﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

public partial class admin_PromoteUser : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        (Master.FindControl("PageSubtitleLabel") as Label).Text = "Promote users to administrators";
    }

    protected void btnLookupUser_Click(object sender, EventArgs e)
    {
        MembershipUserCollection users = Membership.FindUsersByName(txtUserId.Text);
        if (users.Count == 0)
        {
            lblError.Text = "No users matched that ID.  Please try again.";
            pnlResults.Visible = false;
        }
        else
        {
            lblError.Text = "";
            pnlResults.Visible = true;
            lbUsers.DataSource = users;
            lbUsers.DataValueField = "UserName";
            lbUsers.DataTextField = "UserName";
            lbUsers.DataBind();
            lbUsers.SelectedIndex = 0;
        }
    }
    protected void btnPromote_Click(object sender, EventArgs e)
    {
        try
        {
            string u = lbUsers.SelectedValue;
            Roles.AddUserToRole(u, "Admin");
            Page.MessageBox(string.Format("The user, {0}, has been promoted to administrator.", u), "~/admin/Default.aspx");
        }
        catch (Exception ex)
        {
            lblError.Text = ex.Message;
        }
    }
}
